import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:juming_test/module/domain/domain_filter/widgets/domain_filter_conditions.dart';
import 'package:juming_test/module/domain/domain_list/domain_list_logic.dart';

@RoutePage()
class DomainListScreen extends StatefulWidget implements AutoRouteWrapper {
  const DomainListScreen({super.key, @queryParam this.data = ''});

  // 页面传参
  final String data;

  @override
  State<DomainListScreen> createState() => _DomainListScreenState();

  @override
  Widget wrappedRoute(BuildContext context) {
    final DomainListLogic logic = Get.put(DomainListLogic());
    logic.paramsData = data;
    return this;
  }
}

class _DomainListScreenState extends State<DomainListScreen> {
  final DomainListLogic _domainListLogic = Get.put(DomainListLogic());

  @override
  void dispose() {
    super.dispose();
    Get.delete<DomainListLogic>();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CupertinoNavigationBar(
        middle: const Text('列表页面'),
        leading: IconButton(
          onPressed: () {
            context.pop();
          },
          icon: Icon(Icons.arrow_back_ios, size: 20.w),
        ),
        trailing: IconButton(
          onPressed: () {
            _domainListLogic.pushToFilterPage();
          },
          icon: Icon(Icons.filter_alt, size: 20.w),
        ),
        backgroundColor: Colors.white,
      ),
      body: Padding(
        padding: EdgeInsets.all(16.w),
        child: Column(
          children: [
            Obx(() {
              final items =
                  _domainListLogic.domainFilterViewModel.conditionItems.value;
              if (items.isNotEmpty) {
                return DomainFilterConditions(
                  list: items,
                  onDelete: (index) {
                    _domainListLogic.domainFilterViewModel.deleteFilterItem(
                      index,
                    );
                  },
                  pageType: DomainConditionPageType.list,
                );
              }
              return const SizedBox();
            }),
          ],
        ),
      ),
    );
  }
}
